package com.ithailong.reconsitutionmassion.controller;

import com.ithailong.reconsitutionmassion.common.Result;
import com.ithailong.reconsitutionmassion.dao.Impl.UserDaoImpl;
import com.ithailong.reconsitutionmassion.entity.Page;
import com.ithailong.reconsitutionmassion.entity.User;
import com.ithailong.reconsitutionmassion.exception.MyException;
import com.ithailong.reconsitutionmassion.exception.ServicesException;
import com.ithailong.reconsitutionmassion.service.Impl.UserServiceImpl;
import com.ithailong.reconsitutionmassion.util.ConstantMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * author:hlc
 * date:2023/10/29
 *
 * @author 17930
 */
@Controller
@Api(tags = "用户操作接口")
public class UserController {
   @Autowired
   private UserServiceImpl userService;
   @Autowired
   private UserDaoImpl userDao;

   @ApiOperation("用户登录")
   @ResponseBody
   @PostMapping("/login")
   public Result userLogin(@RequestBody User user, Model model, HttpSession session) {
      System.out.println(user);
      model.addAttribute("username", user.getUsername());
      return Result.success(userService.userLogin(user.getUsername(), user.getPassword(), session));
   }

   @ApiOperation("进入主页")
   @RequestMapping("/welcome")
   public String welcome() {
      return "welcome";
   }

   @ApiOperation("用户注册")
   @ResponseBody
   @PostMapping("/register")
   public Result userRegister(@RequestBody User user) {
      System.out.println("我收到请求了");
      return Result.success(userService.register(user));
   }

   @ApiOperation("用户列表")
   @GetMapping("/selectAllUser")
   public ModelAndView selectAllUser(@RequestParam(value = "p", defaultValue = "1") int pageNum,
                                     @RequestParam(value = "s", defaultValue = "6") int pageSize,
                                     User user) {
      List<User> users = userService.userList(pageNum, pageSize, user);
      ModelAndView modelAndView = new ModelAndView();
      //查询用户总数
      int total = userDao.selectTotal(user);
      //构建一个分页对象
      Page page = new Page(pageNum, pageSize, total, users);
      modelAndView.addObject("searchUser", user);
      modelAndView.setViewName("userList");//设置视图名称为userList
      modelAndView.addObject("userList", page);
      return modelAndView;
   }

   //删除用户
   @GetMapping("/delete/{id}")
   public String deleteUser(@PathVariable("id") Integer id, RedirectAttributes attributes, HttpSession session) {
      try {
         boolean b = userService.deleteUser(id, session);
         attributes.addFlashAttribute("message", "删除成功");
         return "redirect:/selectAllUser";
      } catch (MyException e) {
         attributes.addFlashAttribute("message", e.getMessage());
         return "redirect:/selectAllUser";
      }
   }

   //修改用户
   @GetMapping("/update/{id}")
   public String updateUser(@PathVariable("id") Integer id, Model model) {
      User user = userService.selectUserById(id);
      model.addAttribute("editUser", user);
      return "editUser";
   }

   @PostMapping("/edit")
   public String edit(User user, RedirectAttributes attributes) {
      System.out.println(user);
      int i = userService.updateUser(user);
      if (i > 0) {
         attributes.addFlashAttribute("message", "修改成功");
         return "redirect:/selectAllUser";
      } else {
         attributes.addFlashAttribute("message", "修改失败");
         return "redirect:/userList";
      }
   }

   @GetMapping("/disable/{id}")
   public String disable(@PathVariable("id") int id, RedirectAttributes attributes, HttpSession session) {
      System.out.println(id);
      User attribute = (User) session.getAttribute(ConstantMessage.LOGIN_USER);
      User user = userService.selectUserById(id);
      if ("superAdmin".equals(user.getRole())) {
         attributes.addFlashAttribute("message", "你无权禁用超级管理员");
         return "redirect:/selectAllUser";
      }
      if (!"superAdmin".equals(attribute.getRole())) {
         attributes.addFlashAttribute("message", "你无权操作用户状态的权限");
         return "redirect:/selectAllUser";
      }
      user.setId(id);
      if ("0".equals(user.getStatus())) {
         user.setStatus("1");
         System.out.println("========111111");
      } else if ("1".equals(user.getStatus())) {
         user.setStatus("0");
         System.out.println("=========0000");
      }
      userService.updateUser(user);
      return "redirect:/selectAllUser";
   }

   @GetMapping("/default")
   public String defaultPage() {
      return "default";
   }

   //用户退出
   @GetMapping("/userOut")
   public String userOut(HttpServletRequest request) {
      request.getSession().invalidate();
      return "redirect:/";
   }

   @GetMapping("/selectUserById/{id}")
   @ResponseBody
   public Result selectUserById(@PathVariable("id") int id) {
      return Result.success(userService.selectUserById(id));
   }

   @GetMapping("/person")
   public ModelAndView person(HttpSession session) {
      User attribute = (User) session.getAttribute(ConstantMessage.LOGIN_USER);
      int id = attribute.getId();
      User user = userService.selectUserById(id);
      System.out.println(user.toString());
      ModelAndView mv = new ModelAndView();
      mv.setViewName("person");
      mv.addObject("user", user);
      return mv;
   }
}
